package mysql

import "github.com/astaxie/beego/orm"

type DeviceZone struct {
	Id           int    `orm:"Column(ID)"`
	DeviceId     int    `orm:"Column(DeviceID)"`
	DeviceZoneId int    `orm:"Column(DeviceZoneID)"`
	Enable       int    `orm:"Column(Enable)"`
	ZoneClass    string `orm:"Column(ZoneClass)"`
}

func (c *DeviceZone) TableName() string {
	return DeviceZoneTBName()
}

func InsertDeviceZone(dz *DeviceZone) (int64, error) {
	o := orm.NewOrm()
	return o.InsertOrUpdate(dz)
}

func FindDeviceZoneByDevieNameId(deviceNameId string, deviceZoneId int) (*DeviceZone, error) {
	o := orm.NewOrm()
	deviceZone := DeviceZone{}
	err := o.Raw("SELECT zone.* FROM device INNER JOIN zone ON device.ID = zone.DeviceID WHERE device.DeviceNameID =?  AND zone.DeviceZoneID = ?",
		deviceNameId, deviceZoneId).QueryRow(&deviceZone)
	return &deviceZone, err
}

type DeviceDeviceZone struct {
	DeviceZone
	StoreId      string `orm:"Column(StoreId)"`
	DeviceNameId string `orm:"Column(DeviceNameID)"`
	IPaddr       string `orm:"Column(IPaddr)"`
}

func FindAllDeviceDeviceZone() (*[]DeviceDeviceZone, error) {
	o := orm.NewOrm()
	result := new([]DeviceDeviceZone)
	_, err := o.Raw("SELECT zone.*,DeviceNameID,IPaddr,StoreId FROM device INNER JOIN zone ON device.ID = zone.DeviceID").
		QueryRows(result)
	return result, err
}
